//
//  PSStatisticsTests.m
//  PrefCore
//
//  Created by Sergey on 2/23/14.
//  Copyright (c) 2014 Play-on-smart. All rights reserved.
//

#import <XCTest/XCTest.h>
#import "PSStatistics.h"

@interface PSStatisticsTests : XCTestCase

@end

@implementation PSStatisticsTests

-(void)testGameStatsAdd {
    PSGameStatistics *g1 = [[PSGameStatistics alloc] init];
    g1.played = 123;
    g1.pool = 234;
    g1.deleted = 345;
    PSGameStatistics *g2 = [[PSGameStatistics alloc] init];
    g2.played = 987;
    g2.pool = 876;
    g2.deleted = 765;
    XCTAssertEqual((uint64_t)123, g1.played);
    XCTAssertEqual((uint64_t)234, g1.pool);
    XCTAssertEqual((uint64_t)345, g1.deleted);
    [g1 add:g2];
    XCTAssertEqual((uint64_t)(123+987), g1.played);
    XCTAssertEqual((uint64_t)(234+876), g1.pool);
    XCTAssertEqual((uint64_t)(345+765), g1.deleted);
}

-(void)testGameSetStatsAdd {
    PSSetStatistics *s1 = [[PSSetStatistics alloc] init];
    s1.deals = 123;
    s1.passing = 456;
    PSSetStatistics *s2 = [[PSSetStatistics alloc] init];
    s2.deals = 987;
    s2.passing = 654;
    XCTAssertEqual((uint64_t)123, s1.deals);
    XCTAssertEqual((uint64_t)456, s1.passing);
    [s1 add:s2];
    XCTAssertEqual((uint64_t)(123+987), s1.deals);
    XCTAssertEqual((uint64_t)(456+654), s1.passing);
}

-(void)testAggregationsAdd {
    PSAggregationStatistics *s1 = [[PSAggregationStatistics alloc] init];
    s1.sets = 123;
    s1.tricks = 234;
    s1.success = 345;
    PSAggregationStatistics *s2 = [[PSAggregationStatistics alloc] init];
    s2.sets = 987;
    s2.tricks = 876;
    s2.success = 765;
    XCTAssertEqual((uint64_t)123, s1.sets);
    XCTAssertEqual((uint64_t)234, s1.tricks);
    XCTAssertEqual((uint64_t)345, s1.success);
    [s1 add:s2];
    XCTAssertEqual((uint64_t)(123+987), s1.sets);
    XCTAssertEqual((uint64_t)(234+876), s1.tricks);
    XCTAssertEqual((uint64_t)(345+765), s1.success);
}

- (void)testPercentageNonZeroTotal {
    PSPercentage *p = [[PSPercentage alloc] initWithTotal:100 andSuccess:10];
    XCTAssertEqualObjects(@"10%", p.percentageOfSuccess);
    XCTAssertEqualObjects(@"10%", [p description]);
}

- (void)testPercentageZeroTotal {
    PSPercentage *p = [[PSPercentage alloc] initWithTotal:0 andSuccess:0];
    XCTAssertEqualObjects(@"0%", p.percentageOfSuccess);
    XCTAssertEqualObjects(@"0%", [p description]);
}

-(void)testRatioZeroNumDescription {
    PSRatio *r = [[PSRatio alloc] initWithX:0 andY:123];
    XCTAssertEqualObjects(@"0 / 123", [r description]);
}

-(void)testRatioZeroDenomDescription {
    PSRatio *r = [[PSRatio alloc] initWithX:123 andY:0];
    XCTAssertEqualObjects(@"123 / 0", [r description]);
}

-(void)testRatioZeroNumDenomDescription {
    PSRatio *r = [[PSRatio alloc] initWithX:0 andY:0];
    XCTAssertEqualObjects(@"0 / 0", [r description]);
}

-(void)testRatioNonZeroDescription {
    PSRatio *r = [[PSRatio alloc] initWithX:123 andY:246];
    XCTAssertEqualObjects(@"0.50", [r description]);
}

-(void)testStatisticInit {
    PSStatistics *s = [[PSStatistics alloc] init];
    XCTAssertNotNil(s);
    XCTAssertNotNil(s.games);
    XCTAssertNotNil(s.sets);
    XCTAssertTrue(s.score != 0);
    XCTAssertTrue(s.withoutThree != 0);
    XCTAssertTrue(s.passes != 0);
    XCTAssertTrue(s.passingTricks != 0);
    XCTAssertTrue(s.misere != 0);
    XCTAssertTrue(s.whists != 0);
    for (int p = 0 ; p != 3 ; p++) {
        XCTAssertNotNil(s.misere[p]);
        XCTAssertNotNil(s.whists[p]);
    }
}

-(void)testStatisticScore {
    PSStatistics *s = [[PSStatistics alloc] init];
    s.score[0] = 1;
    s.score[1] = 2;
    s.score[2] = 4;
    XCTAssertEqual(s.score[0], 1ULL);
    XCTAssertEqual(s.score[1], 2ULL);
    XCTAssertEqual(s.score[2], 4ULL);
}

-(void)testStatisticWithoutThree {
    PSStatistics *s = [[PSStatistics alloc] init];
    s.withoutThree[0] = 1;
    s.withoutThree[1] = 2;
    s.withoutThree[2] = 4;
    XCTAssertEqual(s.withoutThree[0], 1ULL);
    XCTAssertEqual(s.withoutThree[1], 2ULL);
    XCTAssertEqual(s.withoutThree[2], 4ULL);
}

-(void)testStatisticPasses {
    PSStatistics *s = [[PSStatistics alloc] init];
    s.passes[0] = 1;
    s.passes[1] = 2;
    s.passes[2] = 4;
    XCTAssertEqual(s.passes[0], 1ULL);
    XCTAssertEqual(s.passes[1], 2ULL);
    XCTAssertEqual(s.passes[2], 4ULL);
}

-(void)testStatisticPassingTricks {
    PSStatistics *s = [[PSStatistics alloc] init];
    s.passingTricks[0] = 1;
    s.passingTricks[1] = 2;
    s.passingTricks[2] = 4;
    XCTAssertEqual(s.passingTricks[0], 1ULL);
    XCTAssertEqual(s.passingTricks[1], 2ULL);
    XCTAssertEqual(s.passingTricks[2], 4ULL);
}

-(void)testTrickingSetCount {
    PSStatistics *s = [[PSStatistics alloc] init];
    XCTAssertEqual(s.numberOfTrickingSets, 0ULL);
    [s updateTrickingForPlayerId:1 contract:6 tricks:7];
    XCTAssertEqual(s.numberOfTrickingSets, 1ULL);
    NSArray *tr = [s trickingSetResultForContract:6];
    XCTAssertEqualObjects([tr[0] percentageOfSuccess], @"0%");
    XCTAssertEqualObjects([tr[1] percentageOfSuccess], @"100%");
    XCTAssertEqualObjects([tr[2] percentageOfSuccess], @"0%");
    [s updateTrickingForPlayerId:1 contract:6 tricks:5];
    tr = [s trickingSetResultForContract:6];
    XCTAssertEqualObjects([tr[0] percentageOfSuccess], @"0%");
    XCTAssertEqualObjects([tr[1] percentageOfSuccess], @"50%");
    XCTAssertEqualObjects([tr[2] percentageOfSuccess], @"0%");
}

-(void)testMisereSets {
    PSStatistics *s = [[PSStatistics alloc] init];
    s.misere[0].sets = 25;
    s.misere[0].success = 20;
    s.misere[1].sets = 50;
    s.misere[1].success = 45;
    s.misere[2].sets = 100;
    s.misere[2].success = 99;
    NSArray *res = s.misereSetResult;
    XCTAssertNotNil(res);
    XCTAssertTrue([res[0] isKindOfClass:[PSPercentage class]]);
    XCTAssertEqualObjects([res[0] description], @"80%");
    XCTAssertTrue([res[1] isKindOfClass:[PSPercentage class]]);
    XCTAssertEqualObjects([res[1] description], @"90%");
    XCTAssertTrue([res[2] isKindOfClass:[PSPercentage class]]);
    XCTAssertEqualObjects([res[2] description], @"99%");
}

-(void)testWhistedSets {
    PSStatistics *s = [[PSStatistics alloc] init];
    s.whists[0].sets = 25;
    s.whists[0].success = 20;
    s.whists[1].sets = 50;
    s.whists[1].success = 45;
    s.whists[2].sets = 100;
    s.whists[2].success = 99;
    NSArray *res = s.whistedSets;
    XCTAssertNotNil(res);
    XCTAssertTrue([res[0] isKindOfClass:[PSPercentage class]]);
    XCTAssertEqualObjects([res[0] description], @"80%");
    XCTAssertTrue([res[1] isKindOfClass:[PSPercentage class]]);
    XCTAssertEqualObjects([res[1] description], @"90%");
    XCTAssertTrue([res[2] isKindOfClass:[PSPercentage class]]);
    XCTAssertEqualObjects([res[2] description], @"99%");
}

-(void)testAveragePassingTricks {
    PSStatistics *s = [[PSStatistics alloc] init];
    s.sets.passing = 100;
    s.passingTricks[0] = 25;
    s.passingTricks[1] = 50;
    s.passingTricks[2] = 75;
    NSArray *res = s.averagePassingTricks;
    XCTAssertNotNil(res);
    XCTAssertEqual([res[0] doubleValue], 0.25);
    XCTAssertEqual([res[1] doubleValue], 0.5);
    XCTAssertEqual([res[2] doubleValue], 0.75);
}

-(void)testRatioWhistsToPasses {
    PSStatistics *s = [[PSStatistics alloc] init];
    s.whists[0].sets = 40;
    s.passes[0] = 0;
    s.whists[1].sets = 50;
    s.passes[1] = 50;
    s.whists[2].sets = 60;
    s.passes[2] = 120;
    NSArray *res = s.ratioWhistsToPasses;
    XCTAssertNotNil(res);
    XCTAssertTrue([res[0] isKindOfClass:[PSRatio class]]);
    XCTAssertEqualObjects([res[0] description], @"40 / 0");
    XCTAssertTrue([res[1] isKindOfClass:[PSRatio class]]);
    XCTAssertEqualObjects([res[1] description], @"1.00");
    XCTAssertTrue([res[2] isKindOfClass:[PSRatio class]]);
    XCTAssertEqualObjects([res[2] description], @"0.50");
}

-(void)testStatsDescription {
    PSStatistics *s = [[PSStatistics alloc] init];
    s.numOfGames = 120;
    s.totalPool = 20;
    s.totalAmount = 276;
    NSString *res = [s description];
    XCTAssertEqualObjects(res, @"Games: 120 Pool: 20 Score: 276");
}

@end
